package com.situ.parking;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.JDBC.JDBCUtil;

public class ParkDao {
	/**
	 * 执行insert-sql插入一条记录
	 * 
	 * @param model
	 * @return
	 * @throws SQLException
	 */
	public Integer insert(ParkModel model) {// add save 面向对象的开发
		String sql = "insert into park(plate,state)values(?,?)";
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement ps = null;
		if (conn == null)
			return null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getPlate());
			ps.setInt(2, model.getCount());
			return ps.executeUpdate(); // 执行sql语句，并返回影响的行数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据逻辑主键修改记录其他全部字段
	 * 
	 * @param model
	 * @return
	 */
	public int update(ParkModel model) {
		String sql = "update park set plate=?,state=? where number=?";
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return 0;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getPlate());
			ps.setInt(2, model.getState());
			ps.setInt(3, model.getNumber());
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return 0;
	}

	/**
	 * 根据单个查询条件 - 执行查询sql，返回结果构成的实体类集合
	 * 
	 */
	public ParkModel selectCar(int number, String plate) {
		String sql = "select number,plate,state from park where number = ? and plate = ?";
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		ParkModel b = new ParkModel(null, null);
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, number);
			ps.setString(2, plate);
			rs = ps.executeQuery();
			while (rs.next()) {
				b.setNumber(rs.getInt("number"));
				b.setPlate(rs.getString("plate"));
				b.setState(rs.getInt("state"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}

		return b;
	}

	/**
	 * 根据多个查询条件 - 执行查询sql，返回结果构成的实体类集合
	 * 
	 */
	public List<ParkModel> selectList(ParkModel park) {
		// 要进行字符串拼接，使用StringBuffer/StringBuilder
		StringBuffer sql = new StringBuffer("select number,plate,state from park ");
		List<Object> vals = appendWhere(sql, park);
		Connection conn = JDBCUtil.getConnection();
		if (conn == null)
			return null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<ParkModel> list = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++)
				ps.setObject(i + 1, vals.get(i));
			rs = ps.executeQuery();
			ParkModel b = null;
			while (rs.next()) {// ORM（把数据库的表的信息转换成数据库的过程）
				b = new ParkModel(null, null);
				b.setNumber(rs.getInt("number"));
				b.setPlate(rs.getString("plate"));
				b.setState(rs.getInt("state"));
				list.add(b);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, ParkModel park) {
		sql.append(" where 1=1 ");
		List<Object> vals = new ArrayList<>();
		String plate = park.getPlate();
		if (plate != null && !plate.trim().isEmpty()) { // !code.trim().isEmpty()：不是空字符串（即空格一类的）
			sql.append(" and plate = ? ");
			vals.add(plate.trim());
		}
		Integer state = park.getState();
		if (state != null) {
			sql.append(" and state = ? ");
			vals.add(state);
		}

		return vals;
	}
}
